﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UFIDA.U9.AP.Enums;
using UFIDA.U9.CBO.FI.Enums;
using UFIDA.U9.CBO.SCM.Item;
using UFIDA.U9.MO.Enums;
using UFIDA.U9.SM.SO;
using UFSoft.UBF.Business;
using UFSoft.UBF.Eventing;
using UFSoft.UBF.Util.DataAccess;

namespace UFIDA.U9.CusDev.QJKJPlugInBE
{
    class ProcessForMOLotNo : IEventSubscriber
    {
        public void Notify(params object[] args)
        {

            if (args == null || args.Length == 0 || !(args[0] is UFSoft.UBF.Business.EntityEvent)) return;
            BusinessEntity.EntityKey key = ((UFSoft.UBF.Business.EntityEvent)args[0]).EntityKey;

            if (key == null || key.GetEntity() == null) return;
            //生产订单
            UFIDA.U9.MO.MO.MO mo = key.GetEntity() as UFIDA.U9.MO.MO.MO;
            if (mo == null) return;

            #region 生产订单 批次号唯一性,不为空 
            if (mo.OriginalData.DocState == MOStateEnum.Approved) return;
            ItemMaster item = ItemMaster.Finder.FindByID(mo.ItemMaster.ID);
           if(  item.InventoryInfo.LotParam == null) return; 

            if (mo.DocState == MOStateEnum.Opened)
            {
                //判断来源单据是否为空
                if (mo.SrcDoc.SrcDocNo != null && mo.SrcDoc.SrcDocNo != "" && mo.SrcDoc.MOSrcDocType == MOSrcDocTypeEnum.SO)
                {
                    if (mo.Department == null)
                        throw new Exception("生产部门为空,请填写生产部门");
                 
                    //判断生产批号为空
                    if (mo.ProductLotNo == "" || mo.ProductLotNo == null)
                    {
                        SO so = SO.Finder.FindByID(mo.SrcDoc.SrcDoc.ID);

                        string sql1 = $@"select line.DescFlexField_PrivateDescSeg3  from SM_SO so
                                        left join sm_SOLine line on line.so = so.id
                                        where so.DocNo = '{so.DocNo}' and line.DocLineNo= '{mo.SrcDoc.SrcDocLineNo}'";
                        DataSet ds1 = null;
                        DataAccessor.RunSQL(DataAccessor.GetConn(), sql1, null, out ds1);
                        if (ds1 == null) ds1.Tables[0].Rows[0][0] = false;
                        if (ds1.Tables[0].Rows[0][0].ToString() == "true")
                        {
                            //生产部门为总装车间
                            if (mo.Department.Code == "0902")
                            {
                                //判断为左表
                                if (mo.ItemMaster.Code.Substring(15, 1) == "1")
                                {
                                    string sql = $@"select  max(CONVERT (int,SUBSTRING(mm.ProductLotNo,CHARINDEX('Z',mm.ProductLotNo)+1,DATALENGTH(mm.ProductLotNo))))
                                            from MO_MO mm  
                                            left join CBO_ItemMaster cim on cim.id = mm.ItemMaster 
                                            left join CBO_Department dept on dept.id = mm.Department
                                            where mm.ProductLotNo like '%{so.DocNo}-Z%'  and SUBSTRING(cim.Code,16,1) =1 and dept.code  = '0902'";
                                    DataSet ds = null;
                                    DataAccessor.RunSQL(DataAccessor.GetConn(), sql, null, out ds);
                                    int left;
                                    if (ds.Tables[0].Rows[0][0] == System.DBNull.Value)
                                    {
                                        left = 1;
                                        mo.ProductLotNo = mo.SrcDoc.SrcDocNo +"-EJHY"+ "-Z" + left.ToString();
                                    }
                                    else
                                    {
                                        left = int.Parse(ds.Tables[0].Rows[0][0].ToString()) + 2;
                                        mo.ProductLotNo = mo.SrcDoc.SrcDocNo + "-EJHY" + "-Z" + left.ToString();
                                    }
                                }
                                //判断为右表
                                if (mo.ItemMaster.Code.Substring(15, 1) == "2")
                                {
                                    string sql = $@"select  max(CONVERT (int,SUBSTRING(mm.ProductLotNo,CHARINDEX('Z',mm.ProductLotNo)+1,DATALENGTH(mm.ProductLotNo))))
                                            from MO_MO mm  
                                            left join CBO_ItemMaster cim on cim.id = mm.ItemMaster 
                                            left join CBO_Department dept on dept.id = mm.Department
                                            where mm.ProductLotNo like '%{so.DocNo}-Z%'  and SUBSTRING(cim.Code,16,1) =2 and dept.code  = '0902'";
                                    DataSet ds = null;
                                    DataAccessor.RunSQL(DataAccessor.GetConn(), sql, null, out ds);
                                    int right;
                                    if (ds.Tables[0].Rows[0][0] == System.DBNull.Value)
                                    {
                                        right = 2;
                                        mo.ProductLotNo = mo.SrcDoc.SrcDocNo + "-EJHY" + "-Z" + right.ToString();
                                    }
                                    else
                                    {
                                        right = int.Parse(ds.Tables[0].Rows[0][0].ToString()) + 2;
                                        mo.ProductLotNo = mo.SrcDoc.SrcDocNo + "-EJHY" + "-Z" + right.ToString();
                                    }
                                }

                                //不分左右表
                                if (mo.ItemMaster.Code.Substring(15, 1) == "0")
                                {
                                    string sql = $@"select  max(CONVERT (int,SUBSTRING(mm.ProductLotNo,CHARINDEX('Z',mm.ProductLotNo)+1,DATALENGTH(mm.ProductLotNo))))
                                            from MO_MO mm  
                                            left join CBO_ItemMaster cim on cim.id = mm.ItemMaster 
                                            left join CBO_Department dept on dept.id = mm.Department
                                            where mm.ProductLotNo like '%{so.DocNo}-Z%'  and SUBSTRING(cim.Code,16,1) =0 and dept.code  = '0902'";
                                    DataSet ds = null;
                                    DataAccessor.RunSQL(DataAccessor.GetConn(), sql, null, out ds);
                                    int num;

                                    if (ds.Tables[0].Rows[0][0] == System.DBNull.Value)
                                    {
                                        num = 50;
                                        mo.ProductLotNo = mo.SrcDoc.SrcDocNo + "-EJHY" + "-Z" + num.ToString();
                                    }
                                    else if (int.Parse(ds.Tables[0].Rows[0][0].ToString()) < 50)
                                    {
                                        num = 50;
                                        mo.ProductLotNo = mo.SrcDoc.SrcDocNo + "-EJHY" + "-Z" + num.ToString();
                                    }
                                    else
                                    {
                                        num = int.Parse(ds.Tables[0].Rows[0][0].ToString()) + 1;
                                        mo.ProductLotNo = mo.SrcDoc.SrcDocNo + "-EJHY" + "-Z" + num.ToString();
                                    }
                                }

                            }
                            //生产部门为模块车间
                            else if (mo.Department.Code == "0901")
                            {
                                string sql = $@"select  max(CONVERT (int,SUBSTRING(mm.ProductLotNo,CHARINDEX('M',mm.ProductLotNo)+1,DATALENGTH(mm.ProductLotNo))))
                                            from MO_MO mm  
                                            left join CBO_ItemMaster cim on cim.id = mm.ItemMaster 
                                            left join CBO_Department dept on dept.id = mm.Department
                                            where mm.ProductLotNo like '%{so.DocNo}-M%' and dept.code  = '0901' ";
                                DataSet ds = null;
                                DataAccessor.RunSQL(DataAccessor.GetConn(), sql, null, out ds);
                                int num;
                                if (ds.Tables[0].Rows[0][0] == System.DBNull.Value)
                                {
                                    num = 1;
                                    mo.ProductLotNo = mo.SrcDoc.SrcDocNo + "-EJHY" + "-M" + num.ToString();
                                }
                                else
                                {
                                    num = int.Parse(ds.Tables[0].Rows[0][0].ToString()) + 1;
                                    mo.ProductLotNo = mo.SrcDoc.SrcDocNo + "-EJHY" + "-M" + num.ToString();
                                }
                            }
                            //生产部门为其他
                            else
                            {
                                if (mo.ProductLotNo == null || mo.ProductLotNo == "")
                                {
                                    throw new Exception("请输入生产批号");
                                }
                                else
                                {//生产订单批次号查询
                                    string sql = $@"select mo.ProductLotNo FROM MO_MO mo 
                                where mo.ProductLotNo = '{mo.ProductLotNo}'";
                                    DataSet ds = null;
                                    DataAccessor.RunSQL(DataAccessor.GetConn(), sql, null, out ds);
                                    if (ds.Tables[0].Rows == null || ds.Tables[0].Rows.Count <= 0)
                                    {
                                        return;
                                    }
                                    else
                                    {
                                        throw new Exception($"生产订单批次号重复,生产批号为{mo.ProductLotNo}");
                                    }
                                }
                            }

                        }
                        else
                        {
                            //生产部门为总装车间
                            if (mo.Department.Code == "0902")
                            {
                                //判断为左表
                                if (mo.ItemMaster.Code.Substring(15, 1) == "1")
                                {
                                    string sql = $@"select  max(CONVERT (int,SUBSTRING(mm.ProductLotNo,CHARINDEX('Z',mm.ProductLotNo)+1,DATALENGTH(mm.ProductLotNo))))
                                            from MO_MO mm  
                                            left join CBO_ItemMaster cim on cim.id = mm.ItemMaster 
                                            left join CBO_Department dept on dept.id = mm.Department
                                            where mm.ProductLotNo like '%{so.DocNo}-Z%'  and SUBSTRING(cim.Code,16,1) =1 and dept.code  = '0902'";
                                    DataSet ds = null;
                                    DataAccessor.RunSQL(DataAccessor.GetConn(), sql, null, out ds);
                                    int left;
                                    if (ds.Tables[0].Rows[0][0] == System.DBNull.Value)
                                    {
                                        left = 1;
                                        mo.ProductLotNo = mo.SrcDoc.SrcDocNo + "-Z" + left.ToString();
                                    }
                                    else
                                    {
                                        left = int.Parse(ds.Tables[0].Rows[0][0].ToString()) + 2;
                                        mo.ProductLotNo = mo.SrcDoc.SrcDocNo + "-Z" + left.ToString();
                                    }
                                }
                                //判断为右表
                                if (mo.ItemMaster.Code.Substring(15, 1) == "2")
                                {
                                    string sql = $@"select  max(CONVERT (int,SUBSTRING(mm.ProductLotNo,CHARINDEX('Z',mm.ProductLotNo)+1,DATALENGTH(mm.ProductLotNo))))
                                            from MO_MO mm  
                                            left join CBO_ItemMaster cim on cim.id = mm.ItemMaster 
                                            left join CBO_Department dept on dept.id = mm.Department
                                            where mm.ProductLotNo like '%{so.DocNo}-Z%'  and SUBSTRING(cim.Code,16,1) =2 and dept.code  = '0902'";
                                    DataSet ds = null;
                                    DataAccessor.RunSQL(DataAccessor.GetConn(), sql, null, out ds);
                                    int right;
                                    if (ds.Tables[0].Rows[0][0] == System.DBNull.Value)
                                    {
                                        right = 2;
                                        mo.ProductLotNo = mo.SrcDoc.SrcDocNo + "-Z" + right.ToString();
                                    }
                                    else
                                    {
                                        right = int.Parse(ds.Tables[0].Rows[0][0].ToString()) + 2;
                                        mo.ProductLotNo = mo.SrcDoc.SrcDocNo + "-Z" + right.ToString();
                                    }
                                }

                                //不分左右表
                                if (mo.ItemMaster.Code.Substring(15, 1) == "0")
                                {
                                    string sql = $@"select  max(CONVERT (int,SUBSTRING(mm.ProductLotNo,CHARINDEX('Z',mm.ProductLotNo)+1,DATALENGTH(mm.ProductLotNo))))
                                            from MO_MO mm  
                                            left join CBO_ItemMaster cim on cim.id = mm.ItemMaster 
                                            left join CBO_Department dept on dept.id = mm.Department
                                            where mm.ProductLotNo like '%{so.DocNo}-Z%'  and SUBSTRING(cim.Code,16,1) =0 and dept.code  = '0902'";
                                    DataSet ds = null;
                                    DataAccessor.RunSQL(DataAccessor.GetConn(), sql, null, out ds);
                                    int num;

                                    if (ds.Tables[0].Rows[0][0] == System.DBNull.Value)
                                    {
                                        num = 50;
                                        mo.ProductLotNo = mo.SrcDoc.SrcDocNo + "-Z" + num.ToString();
                                    }
                                    else if (int.Parse(ds.Tables[0].Rows[0][0].ToString()) < 50)
                                    {
                                        num = 50;
                                        mo.ProductLotNo = mo.SrcDoc.SrcDocNo + "-Z" + num.ToString();
                                    }
                                    else
                                    {
                                        num = int.Parse(ds.Tables[0].Rows[0][0].ToString()) + 1;
                                        mo.ProductLotNo = mo.SrcDoc.SrcDocNo + "-Z" + num.ToString();
                                    }
                                }

                            }
                            //生产部门为模块车间
                            else if (mo.Department.Code == "0901")
                            {
                                string sql = $@"select  max(CONVERT (int,SUBSTRING(mm.ProductLotNo,CHARINDEX('M',mm.ProductLotNo)+1,DATALENGTH(mm.ProductLotNo))))
                                            from MO_MO mm  
                                            left join CBO_ItemMaster cim on cim.id = mm.ItemMaster 
                                            left join CBO_Department dept on dept.id = mm.Department
                                            where mm.ProductLotNo like '%{so.DocNo}-M%' and dept.code  = '0901' ";
                                DataSet ds = null;
                                DataAccessor.RunSQL(DataAccessor.GetConn(), sql, null, out ds);
                                int num;
                                if (ds.Tables[0].Rows[0][0] == System.DBNull.Value)
                                {
                                    num = 1;
                                    mo.ProductLotNo = mo.SrcDoc.SrcDocNo + "-M" + num.ToString();
                                }
                                else
                                {
                                    num = int.Parse(ds.Tables[0].Rows[0][0].ToString()) + 1;
                                    mo.ProductLotNo = mo.SrcDoc.SrcDocNo + "-M" + num.ToString();
                                }
                            }
                            //生产部门为其他
                            else
                            {
                                if (mo.ProductLotNo == null || mo.ProductLotNo == "")
                                {
                                    throw new Exception("请输入生产批号");
                                }
                                else
                                {//生产订单批次号查询
                                    string sql = $@"select mo.ProductLotNo FROM MO_MO mo 
                                where mo.ProductLotNo = '{mo.ProductLotNo}'";
                                    DataSet ds = null;
                                    DataAccessor.RunSQL(DataAccessor.GetConn(), sql, null, out ds);
                                    if (ds.Tables[0].Rows == null || ds.Tables[0].Rows.Count <= 0)
                                    {
                                        return;
                                    }
                                    else
                                    {
                                        throw new Exception($"生产订单批次号重复,生产批号为{mo.ProductLotNo}");
                                    }
                                }
                            }

                        }
                      
                      
                    }
                    //生产批号不为空的判重
                    else
                    {
                        //生产订单批次号查询
                        string sql = $@"select mo.ProductLotNo FROM MO_MO mo 
                                where mo.ProductLotNo = '{mo.ProductLotNo}' ";
                        DataSet ds = null;
                        DataAccessor.RunSQL(DataAccessor.GetConn(), sql, null, out ds);
                        if (ds.Tables[0].Rows == null || ds.Tables[0].Rows.Count <= 0)
                        {
                            return;
                        }
                        else
                        {
                            throw new Exception($"生产订单批次号重复,生产批号为{mo.ProductLotNo}");
                        }
                    }             
                    
                }
                //来源单据为空
                else
                {
                    if (mo.ProductLotNo == null || mo.ProductLotNo == "")
                    {
                        throw new Exception("请输入生产批号");
                    }
                    else
                    {
                        //生产订单批次号查询
                        string sql = $@"select mo.ProductLotNo FROM MO_MO mo 
                                where mo.ProductLotNo = '{mo.ProductLotNo}' ";
                        DataSet ds = null;
                        DataAccessor.RunSQL(DataAccessor.GetConn(), sql, null, out ds);
                        if (ds.Tables[0].Rows == null || ds.Tables[0].Rows.Count <= 0)
                        {
                            return;
                        }
                        else
                        {
                            throw new Exception($"生产订单批次号重复,生产批号为{mo.ProductLotNo}");
                        }
                    }

                }

            }
            else
            {
                return;
            }
        }
            #endregion
    }
}
